import SockJS from "sockjs-client";
import Stomp from "stompjs"
import $const from "./ServerPath"

export function initWebSocket(that) {
  let socket = new SockJS($const.APP_SERVER + "/endpointOne");
  let stomp = Stomp.over(socket);
  stomp.connect({}, (frame) => {
    stomp.subscribe("/topic/add/order", (res) => {
      let str = "订单";
      let orderAudio = new Audio($const.WORK_SERVER + "/profile/newOrder.mp3"); //新订单铃声
      let msgAudio = new Audio($const.WORK_SERVER + "/profile/bxnew.mp3");      //新消息铃声
      let arr = res.body.split("_");
      let send = 30000;
      if(arr[0] === "collect") {
        str = "回收订单"
        orderAudio.play();
      }else if(arr[0] === "msg") {
        str = "待处理订单"
        msgAudio.play();
        send = 0;
      }else if(arr[0] === "sale") {
        str = "消费订单"
        orderAudio.play();
      }else if(arr[0] === "store") {
        str = "门店回收"
        msgAudio.play()
      }
      that.$notify.info({
        title: str,
        message: arr[1],
        duration: send
      })
      that.getList();
    })
  })
}
